<template>
  <div class='xtx-carousel'
    @mouseenter="close"
    @mouseleave="start">
    <ul class="carousel-body">
      <li class="carousel-item"
        v-for="(item,i) in sliders"
        :class="{fade:index===i}"
        :key="item.id">
        <RouterLink :to="item.hrefUrl">
          <img :src="item.imgUrl"
            alt="">
        </RouterLink>
      </li>
    </ul>
    <a href="javascript:;"
      @click="toggle(-1)"
      class="carousel-btn prev"><i class="iconfont icon-angle-left"></i></a>
    <a href="javascript:;"
      @click="toggle(1)"
      class="carousel-btn next"><i class="iconfont icon-angle-right"></i></a>
    <div class="carousel-indicator">
      <span v-for="(item,i) in sliders"
        :class="{active:index===i}"
        @click="index=i"
        :key="item.id"></span>
    </div>
  </div>
</template>

<script>
// 实现轮播图功能
export default {
  name: 'XtxCarousel',
  props: {
    sliders: {
      type: Array,
      default: () => []
    },
    // 轮播间隔时长
    durantion: {
      type: Number,
      default: 2000
    },
    // 是否自动播放
    autoPlay: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      // 通过索引切换轮播图  默认为第一张
      index: 0
    }
  },
  //  自动播放功能实现----监听sliders是否有数据，有数据之后才开始自动播放
  watch: {
    sliders (newVal, oldVal) {
      // 判断newVal是否有值才开始自动播放
      if (newVal && newVal.length) {
        // 控制自动播放
        this.autoPlay && this.autoPlayFn()
      }
    }
  },
  methods: {
    // 自动播放
    autoPlayFn () {
      // 定义一个定时器，隔一段时间去切换一下索引
      // this.timer不需要声明，直接挂在到实例上就ok 因为组件销毁的时候会把定时器全部清除掉 ----  细节
      clearInterval(this.timer)
      this.timer = setInterval(() => {
        this.index++
        if (this.index >= this.sliders.length) {
          this.index = 0
        }
      }, this.durantion)
    },
    // 定义轮播图左右按钮 用一个方法解决
    toggle (step) {
      this.index += step
      if (this.index < 0) {
        this.index = this.sliders.length - 1
      }
      if (this.index >= this.sliders.length) {
        this.index = 0
      }
    },
    // 鼠标经过的时候关闭定时器
    close () {
      clearInterval(this.timer)
    },
    // 鼠标离开时开始定时器
    start () {
      this.autoPlay && this.autoPlayFn()
    }
  }
}
</script>

<style lang="less" scoped>
.xtx-carousel {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>
